package com.ellen.mac.chatdemo.viewholder;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.avos.avoscloud.im.v2.AVIMMessage;
import com.avos.avoscloud.im.v2.messages.AVIMTextMessage;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ellen.mac.chatdemo.R;
import com.ellen.mac.chatdemo.adpter.NewImageAdapter;
import com.ellen.mac.chatdemo.adpter.NewVoiceAdapter;
import com.ellen.mac.chatdemo.model.AVDataBean;
import com.ellen.mac.chatdemo.model.DataMP3Bean;
import com.ellen.mac.chatdemo.model.DataPhotoBean;
import com.ellen.mac.chatdemo.model.TeacherData;
import com.ellen.mac.chatdemo.utils.LogUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.yanzhenjie.album.Action;
import com.yanzhenjie.album.Album;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by wli on 15/9/17.
 * 聊天页面中的文本 item 对应的 holder
 */
public class NewChatItemTextHolder extends ChatItemHolder implements BaseQuickAdapter.OnItemChildClickListener {

    protected TextView contentView;
    protected RecyclerView voice, photos;
    private Gson gson;
    private GridLayoutManager VoicelayoutManage, PhotolayoutManage;
    private NewVoiceAdapter voiceAdapter;
    private NewImageAdapter imageAdapter;
    private Context context;
    private List<DataMP3Bean> list;
    private List<DataPhotoBean> photoList;

    public NewChatItemTextHolder(Context context, ViewGroup root, boolean isLeft) {
        super(context, root, isLeft);
        gson = new Gson();
        this.context = context;
        list = new ArrayList<>();
        photoList = new ArrayList<>();
        VoicelayoutManage = new GridLayoutManager(context, 1);
        PhotolayoutManage = new GridLayoutManager(context, 1);
        imageAdapter = new NewImageAdapter(R.layout.chat_item_image_layout, photoList, context);
        if (isLeft) {
            voiceAdapter = new NewVoiceAdapter(R.layout.new_item_play_voice_item, list, context);
        } else {
            voiceAdapter = new NewVoiceAdapter(R.layout.chat_item_right_audio_layout, list, context);
        }
        voice.setLayoutManager(VoicelayoutManage);
        photos.setLayoutManager(PhotolayoutManage);
        voice.setAdapter(voiceAdapter);
        photos.setAdapter(imageAdapter);

        imageAdapter.setOnItemChildClickListener(this);
    }

    @Override
    public void initView() {
        super.initView();
        if (isLeft) {
            conventLayout.addView(View.inflate(getContext(), R.layout.new_chat_item_left_layout, null));
            contentView = itemView.findViewById(R.id.left_text);
            voice = itemView.findViewById(R.id.left_voice);
            photos = itemView.findViewById(R.id.left_photos);


        } else {
            conventLayout.addView(View.inflate(getContext(), R.layout.new_chat_item_right_layout, null));
            contentView = itemView.findViewById(R.id.right_text);
            voice = itemView.findViewById(R.id.right_voice);
            photos = itemView.findViewById(R.id.right_photos);

        }

    }

    @Override
    public void bindData(Object o) {
        super.bindData(o);
        AVIMMessage message = (AVIMMessage) o;
        TeacherData data = null;
        try {
            if (message instanceof AVIMTextMessage) {
                AVIMTextMessage textMessage = (AVIMTextMessage) message;
                data = gson.fromJson(textMessage.getText(), TeacherData.class);
                if (data != null) {
                    contentView.setText(data.getText());
                    list = data.getAudio();
                    photoList = data.getImage();
                }
                imageAdapter.setNewData(photoList);
                imageAdapter.notifyDataSetChanged();
                voiceAdapter.setNewData(list);
                voiceAdapter.notifyDataSetChanged();
            }else{
                getBeanDtat(message.getContent());
            }

        } catch (JsonSyntaxException e) {
            LogUtils.v(e.toString() + "---data");
            getBeanDtat(message.getContent());
        }
    }

    private void getBeanDtat(String message) {
        AVDataBean bean = null;
        try {
            bean = gson.fromJson(message, AVDataBean.class);
        } catch (JsonSyntaxException e) {
            LogUtils.v(e.toString() + "--bean");
        } finally {
            if (bean.get_lctype() != 0) {
                contentView.setText(bean.get_lctext());
                DataPhotoBean photoBean = new DataPhotoBean();
                DataMP3Bean mp3Bean = new DataMP3Bean();
                if (bean.get_lctype() == -3) {
                    mp3Bean.setUrl(bean.get_lcfile().getUrl());
                    mp3Bean.setSize(bean.get_lcfile().getMetaData().getSize());
                    mp3Bean.setDuration((long) bean.get_lcfile().getMetaData().getDuration());
                    list.add(mp3Bean);
                } else if (bean.get_lctype() == -2) {
                    photoBean.setUrl(bean.get_lcfile().getUrl());
                    photoBean.setHeight(bean.get_lcfile().getMetaData().getHeight());
                    photoBean.setWidth(bean.get_lcfile().getMetaData().getWidth());
                    photoBean.setSize(bean.get_lcfile().getMetaData().getSize());
                    photoList.add(photoBean);
                }
            }
            imageAdapter.setNewData(photoList);
            imageAdapter.notifyDataSetChanged();
            voiceAdapter.setNewData(list);
            voiceAdapter.notifyDataSetChanged();
        }
    }


    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        ArrayList<String> urls = new ArrayList<>();
        for (int i = 0; i < photoList.size(); i++) {
            urls.add(photoList.get(i).getUrl());
        }
        Album.gallery(context)
                .requestCode(100) // 请求码，会在listener中返回。
                .checkedList(urls) // 要浏览的图片列表：ArrayList<String>。
                .currentPosition(position) // 预览的时候要显示list中的图片的index。
                .navigationAlpha(100) // Android5.0+的虚拟导航栏的透明度。
                .checkable(false) // 是否有浏览时的选择功能。
                .onResult(new Action<ArrayList<String>>() { // 如果checkable(false)，那么action不用传。
                    @Override
                    public void onAction(int requestCode, @NonNull ArrayList<String> result) {

                    }
                })
                .onCancel(new Action<String>() {
                    @Override
                    public void onAction(int requestCode, @NonNull String result) {
                    }
                })
                .start(); // 千万不要忘记调用start()方法。
    }
}
